package com.um.jdy.tcp.sdk;

import cn.hutool.core.util.IdUtil;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.device.dao.EbLogDao;
import com.um.jdy.common.device.dao.PileDao;
import com.um.jdy.common.device.entity.po.EbLog;
import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.tcp.base.BaseCommand;
import com.um.jdy.tcp.cmd.CommandLogs;
import com.um.jdy.tcp.cmd.SDKCommand;
import com.um.jdy.tcp.cmd.SDKCommandParser;
import com.um.jdy.tcp.properties.HshNettyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 邓南辉
 * 日期 2023/8/28
 * 版权所有 广州优匠科技有限公司
 */
@Service
public class CommandLogsImpl implements CommandLogs {

    @Autowired
    PileDao pileDao;

    @Autowired
    EbLogDao ebLogDao;

    @Autowired
    SDKCommandParser sdkCommandParser;

    @Autowired
    HshNettyProperties hshNettyProperties;

    @Async
    @Override
    public void addLogs(String imei, String command, Date time,boolean status) {
        if (!hshNettyProperties.isWriteLog())
            return;

        BaseCommand cmd = (BaseCommand) sdkCommandParser.parse(command);
        Pile devicePile = pileDao.getEntity("imei", imei);
        EbLog log = new EbLog();
        log.setComId(devicePile == null ? "" : devicePile.getComId());
        log.setCode(devicePile == null ? "" : devicePile.getCode());
        log.setImei(imei);
        log.setCmd(cmd.getCmd() + "");
        log.setSeq(cmd.getSeq());
        log.setIdentifier(cmd.getClass().getSimpleName());
        log.setRequestData(command);
        log.setCreateTime(time);
        log.setStatus(status ? YesOrNo.Yes.name() : YesOrNo.No.name());
        log.setDeviceEbLogId(IdUtil.fastSimpleUUID());
        ebLogDao.addRecord(log);
    }

    @Async
    @Override
    public void addLogs(String imei, SDKCommand cmd, String commandStr, Date time,boolean status) {
        if (!hshNettyProperties.isWriteLog())
            return;

        BaseCommand command = (BaseCommand) cmd;
        Pile devicePile = pileDao.getEntity("imei", imei);
        EbLog log = new EbLog();
        log.setComId(devicePile == null ? "" : devicePile.getComId());
        log.setCode(devicePile == null ? "" : devicePile.getCode());
        log.setImei(imei);
        log.setCmd(command.getCmd() + "");
        log.setSeq(command.getSeq());
        log.setIdentifier(command.getClass().getSimpleName());
        log.setRequestData(commandStr);
        log.setCreateTime(time);
        log.setStatus(status ? YesOrNo.Yes.name() : YesOrNo.No.name());
        log.setDeviceEbLogId(IdUtil.fastSimpleUUID());
        ebLogDao.addRecord(log);
    }

}
